import json
from time import sleep
from selenium import webdriver
import pytest
from selenium.webdriver.chrome.service import Service

from common.Loger_log import write_loging
from common.yaml_util import read_config_file

options = webdriver.ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_argument("--window-size=1920,1080")  # 设置窗口大小，避免某些网站在无头模式下布局错误
options.add_argument("--no-sandbox")#解决DevToolsActivePort文件不存在的报错
#options.add_argument("--headless") # 浏览器不提供可视化页面. windows下如果系统不支持
options.add_experimental_option("prefs",{"credentials_enable_service":False,"profile.password_manager_enabled":False})
from pages.user_login_page import UserLoginPage
service = Service(executable_path='/Users/smzdm/Documents/chromedriver')
@pytest.fixture(scope="session")
def login_name(): #登录保存cookie
    '/Users/smzdm/Documents/chromedriver'

    driver=webdriver.Chrome(options=options,service=service)
    driver.get(read_config_file('base','dic_8013')+"/web/index.html#/login")
    UserLoginPage(driver).username_pwd("keepa1234","mawenqiang")
    sleep(5)
    local_storage_content = driver.execute_script("return JSON.stringify(window.localStorage);")
    write_loging("前置条件保存cookie成功")
    # 将内容保存到本地文件
    with open('localStorage.json', 'w') as file:
        file.write(local_storage_content)
    print("保存成功")
    yield driver
    driver.quit()


@pytest.fixture(scope="session")
def setup_browser():
    driver = webdriver.Chrome(options=options,service=service)
    yield driver
    driver.quit()

# def test_cese(login_name):#调试 用的
#         print(123213124)

if __name__ == '__main__':
    pass